package com.framework.buss.ai.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.framework.buss.ai.constant.BussConstant;
import com.framework.buss.ai.mapper.QuestionMapper;
import com.framework.buss.ai.model.Question;
import com.framework.buss.ai.params.QuestionParam;
import com.framework.buss.ai.service.IQuestionService;
import com.framework.buss.ai.vo.QuestionVO;
import com.framework.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author zhangzhg
 */
@Service
public class QuestionService implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Override
    public IPage<QuestionVO> list(QuestionParam param, Page page) {
        String diseaseType =  param.getDiseaseType();
        // 支持前端查询全部情况下， 按病害类型分组全部展示
        if (StringUtils.isNotBlank(diseaseType) && diseaseType.indexOf(BussConstant.SPLIT_COMMA) > 0) {
            List<String> types = Arrays.asList(diseaseType.split(BussConstant.SPLIT_COMMA));
            param.setDiseaseTypeList(types);
            param.setDiseaseType(null);
        }
        // 根据病害类型查询
        IPage<QuestionVO> list = questionMapper.selectListPage(page, param);
        return list;
    }

    @Override
    public List<QuestionVO> list(QuestionParam param) {
        return questionMapper.selectList(param);
    }

    @Override
    public String save(Question param) {
        String id = param.getId();
        if (StringUtils.isBlank(id)) {
            questionMapper.insert(param);
        } else {
            questionMapper.updateById(param);
        }

        return param.getId();
    }

    @Override
    public void del(List<String> id) {
        questionMapper.deleteBatchIds(id);
    }

    @Override
    public Question getOne(String id) {
        return questionMapper.selectById(id);
    }
}
